﻿/**
 * 项目名称: DouDiZhu
 * 版本号：1.0
 * 名字：雷文
 * 官网：http://FansUnion.cn
 * CSDN:http://blog.csdn.net/FansUnion
 * 邮箱: FansUnion@qq.com
 * QQ：240370818
 * 版权所有: 2013-2113,LeiWen
 */
package cn.fansunion.ddz;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

import cn.fansunion.ddz.client.Card;
import cn.fansunion.ddz.client.CardType;
import cn.fansunion.ddz.client.CardUtil;
import cn.fansunion.ddz.client.DealingDevice;
import cn.fansunion.ddz.client.GameRule;


/**
 * 斗地主客户端
 * 
 * @author FansUnion@qq.com,http://FansUnion.cn,
 *         http://blog.csdn.net/FansUnion
 * 
 */
public class DdzGUI extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JPanel contentPane;

	// 我的牌
	public List<Card> myCards = new ArrayList<Card>();

	private List<Card> allCards = new ArrayList<Card>(54);

	public List<Card> leftCards = new ArrayList<Card>(17);// 左边玩家的牌

	public List<Card> rightCards = new ArrayList<Card>(17);// 右边玩家的牌

	public List<Card> topCards = new ArrayList<Card>(3);// 底牌

	public List<Card> copyOfTopCards = new ArrayList<Card>(3);// 底牌

	// 我选中的牌，想出的牌
	public List<Card> selectedCards = new ArrayList<Card>();

	// 上家出的牌
	public List<Card> prevCards = new ArrayList<Card>();

	// 按钮
	private JButton jiaoDiZhu, buJiao, chuPai, buChu;

	// 默认的背景色
	public static Color DEFAULT_BGCOLOR = new Color(232, 220, 184);

	public DdzGUI() {
		enableEvents(AWTEvent.WINDOW_EVENT_MASK);
		init();
	}

	private void initGUI() {

		BorderLayout borderLayout = new BorderLayout();
		contentPane = (JPanel) getContentPane();
		contentPane.setLayout(borderLayout);
		this.setSize(new Dimension(800, 600));

		this.setLayout(null);
		contentPane.setBackground(Color.CYAN);

		Icon back_icon = DdzUtil.getImageIcon("back.gif");
		JLabel back_label = new JLabel("", back_icon, JLabel.CENTER);
		back_label.setBounds(0, 0, 800, 600);
		contentPane.add(back_label);
		contentPane.setBackground(DEFAULT_BGCOLOR);
		initButtons();
		try {
			initPanel();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}// 初始化 自己的界面

	}

	private void init() {
		DealingDevice one = new DealingDevice();
		allCards = one.getAllCards();

		// 1号玩家的牌
		for (int j = 0; j < 17; j++) {
			Card card = allCards.get(j * 3);
			myCards.add(card);
		}

		// 2号玩家的牌
		for (int j = 0; j < 17; j++) {
			Card card = allCards.get(j * 3 + 1);
			rightCards.add(card);
		}

		// 3号玩家的牌
		for (int j = 0; j < 17; j++) {
			Card card = allCards.get(j * 3 + 2);
			leftCards.add(card);
		}

		// 底牌
		for (int i = 51; i < 54; i++) {
			Card card = allCards.get(i);
			topCards.add(card);

			Card card2 = new Card(card.id);
			copyOfTopCards.add(card2);
		}

		CardUtil.sortCards(myCards);
		CardUtil.sortCards(rightCards);
		CardUtil.sortCards(leftCards);
	}

	@Override
	protected void processWindowEvent(WindowEvent e) {
		super.processWindowEvent(e);
		if (e.getID() == WindowEvent.WINDOW_CLOSING) {
			System.exit(0);
		}
	}

	private void initPanel() throws InterruptedException {
		// 初始化 自己的界面
		for (int i = 0; i < 17; i++) {
			myCards.get(i).setBounds(400 - i * 15, 470, 71, 96);
			contentPane.add(myCards.get(i));
			myCards.get(i).addMouseListener(
					new CardsMouseAdapter(myCards.get(i), this));

			rightCards.get(i).setBounds(710, 420 - i * 25, 71, 96);
			contentPane.add(rightCards.get(i));

			leftCards.get(i).setBounds(20, 420 - i * 25, 71, 96);
			contentPane.add(leftCards.get(i));
			validate();
			repaint();

		}

		for (int i = 0; i < 3; i++) {
			topCards.get(i).setBounds(430 - i * 80, 20, 71, 96);
			contentPane.add(topCards.get(i));
			topCards.get(i).addMouseListener(
					new CardsMouseAdapter(topCards.get(i), this));
		}

		contentPane.add(jiaoDiZhu);
		contentPane.add(buJiao);
		contentPane.add(chuPai);
		contentPane.add(buChu);
	}

	/**
	 * 初始化按钮
	 * 
	 */
	private void initButtons() {
		chuPai = new JButton("出牌");
		chuPai.setBounds(520, 430, 70, 30);
		chuPai.addActionListener(this);

		buChu = new JButton("不出");
		buChu.setBounds(600, 430, 70, 30);
		buChu.addActionListener(this);

		jiaoDiZhu = new JButton("叫地主");
		jiaoDiZhu.setBounds(370, 430, 90, 30);
		jiaoDiZhu.addActionListener(this);

		buJiao = new JButton("不叫");
		buJiao.setBounds(450, 430, 70, 30);
		buJiao.addActionListener(this);
	}

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			DdzGUI mainFrame = new DdzGUI();
			mainFrame.initGUI();
			showGUI(mainFrame, 800, 600);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 显示用户界面
	 * 
	 * @param frame
	 * @param width
	 * @param height
	 */
	public static void showGUI(JFrame frame, int width, int height) {
		frame.setTitle("斗地主--小雷FansUnion-博学的互联网技术工作者-http://FansUnion.cn");
		frame.setIconImage(DdzUtil.getAppIcon());
		frame.setResizable(false);
		frame.setSize(width, height);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);

	}

	public class CardsMouseAdapter extends MouseAdapter {

		Card card = null;

		DdzGUI owner;

		public void mouseClicked(MouseEvent e) {
			if (card.getBounds().y == 470) {
				card.setBounds(card.getBounds().x, 470 - 20, 71, 96);
				System.out
						.println("选中的牌增加了一张：" + card.bigType + card.smallType);
				owner.selectedCards.add(card);

			} else if (card.getBounds().y == 450) {
				card.setBounds(card.getBounds().x, 470, 71, 96);
				System.out
						.println("选中的牌减少了一张：" + card.bigType + card.smallType);
				owner.selectedCards.remove(card);
			}
			System.out.println("牌的X坐标:" + card.getBounds().getCenterX());
		}

		public CardsMouseAdapter(Card labels, DdzGUI owner) {
			super();
			this.card = labels;
			this.owner = owner;
		}

	}

	/**
	 * 响应事件
	 */
	public void actionPerformed(ActionEvent event) {
		Object source = event.getSource();

		if (source == chuPai) {
			CardType cardType = GameRule.getCardType(selectedCards);
			boolean isOvercome = GameRule.isOvercomePrev(selectedCards,
					cardType, prevCards, cardType);// 暂时假定上一首牌和这一首牌的类型相同
			if (isOvercome) {
				GameRule.print("我出的牌大过上家。");
			} else {
				JOptionPane.showMessageDialog(this, "我出的牌不能大过上家", "我出的牌不能大过上家",
						JOptionPane.YES_OPTION);
				GameRule.print("我出的牌不能大过上家。");
			}

			if (cardType != null) {
				DdzUtil.playSound(selectedCards, cardType);
				int size = selectedCards.size();

				if (prevCards != null) {
					for (int j = 0; j < prevCards.size(); j++) {
						remove(prevCards.get(j));
						System.out.println("删除" + prevCards.get(j));
					}
					
					prevCards.clear();
					prevCards.addAll(selectedCards);

					CardUtil.sortCards(prevCards);
					for (int i = 0; i < size; i++) {
						prevCards.get(i).setBounds(400 - i * 15, 350, 71, 96);
					}
				}
				
				myCards.removeAll(selectedCards);

				validate();
				repaint();
				selectedCards.clear();

				sortAndUpdateMyCards();

				// 测试用的，根据上一首牌，是否灰化出牌按钮
				boolean flag = GameRule.isOvercomePrev(myCards, prevCards,
						cardType);
				if (flag) {
					chuPai.setEnabled(true);
				} else {
					chuPai.setEnabled(false);
				}

			} else {
				JOptionPane.showMessageDialog(this, "不符合规则", "不符合规则",
						JOptionPane.YES_OPTION);
			}
		} else if (source == jiaoDiZhu) {
			jiaoDiZhu.setEnabled(false);// 灰化叫地主按钮
			buJiao.setEnabled(false);
			myCards.addAll(topCards);// 将底牌加入到我的牌中

			selectedCards.addAll(topCards);// 选中的牌增加3张

			sortAndUpdateMyCards();// 更新我的牌
			topCards.clear();//
			DdzUtil.playSound("jiaodizhu.wav");

			// 底牌的位置
			for (int i = 0; i < 3; i++) {
				copyOfTopCards.get(i).setBounds(430 - i * 80, 20, 71, 96);
				contentPane.add(copyOfTopCards.get(i));
			}
		}
	}

	/**
	 * 排序并更新我的牌
	 * 
	 */
	public void sortAndUpdateMyCards() {
		CardUtil.sortCards(myCards);

		int size = myCards.size();
		for (int i = 0; i < size; i++) {
			Card card = myCards.get(i);
			if (topCards.contains(card)) {
				myCards.get(i).setBounds(400 - i * 15, 470 - 20, 71, 96);
			} else {
				myCards.get(i).setBounds(400 - i * 15, 470, 71, 96);
			}
			contentPane.add(myCards.get(i));
		}

	}
}